tags = {
  "NAV" => IO.read("_nav.html")
}
swf_copies = {
  "buttons/basic_button.swf" => [
                                  "buttons/basic_button_link.swf",
                                  "buttons/basic_button_event.swf", 
                                  "buttons/basic_button_event2.swf",
                                  "buttons/basic_button_instance_shortcut.swf",
                                  "buttons/basic_button_link.swf"
  ],
  "more_buttons/state_button.swf" => [
                                  "more_buttons/state_button_loop.swf",
                                  "more_buttons/state_button_transition.swf"
  ],
  "slider/slider.swf" => [
                                  "slider/to_click_slider.swf"
  ],
  "actions/actions.swf" => [
                                  "actions/trading_actions.swf"
  ],
  "tweens/tweens.swf" => [
                                  "tweens/auto_tweens.swf"
  ]
}

publish = false

flas = []
Dir.foreach(Dir.pwd) do |item| 
  if File.directory?(item) and item != ".."
    # collect names of all fla files
    Dir.foreach(item) do |f|
      if /\.fla$/.match(f)
        flas.push "file://#{File.expand_path(item + '/' + f)}"
      end
    end
  elsif /\.html$/.match(item) and !/^_/.match(item)
    # update the navigation
    html = File.read(item)
    tags.each_pair do |t, v|
      html.gsub!(/(<!--\s*\{#{t}\}\s*-->).*(<!--\s*\{\/#{t}\}\s*-->)/m, "<!-- {#{t}} -->\n" + v + "\n<!-- {/#{t}} -->")
    end
    File.open(item, "w" ) do |f|
      f.write html
    end
  end
end

# build the jsfl script to publish 
# all the fla files and write out 
# the jsfl file
jsfl = "var doc = null\n"
flas.each do |fla|
  jsfl += "doc = fl.openDocument(\"#{fla}\")\n" 
  jsfl += "doc.exportSWF(\"#{fla.sub(/\.fla$/, ".swf")}\", true)\n"
  jsfl += "fl.closeAll(false)\n"
end
File.open("_build_docs.jsfl", "w" ) do |f|
  f.write jsfl
end

# execute the jsfl script to publish the flas
if publish
  `osascript -e 'tell application "Finder" to open file "Macintosh HD:Users:Matthew:Development:Flash:actionml:doc:_build_docs.jsfl" '`
end

# copy the swfs that are used with multiple xml files
swf_copies.each_pair do |k, v|
  v.each do |i|
    `cp #{k} #{i}`
  end
end

Dir.foreach(Dir.pwd) do |item| 
  if File.directory?(item) and item != ".."
    if File.directory?("#{item}/_notes")
      `rm -r #{item}/_notes`
    end
  end
end

